import { RouteLocationNormalized, RouteRecordRaw } from 'vue-router';
import { CacheEnum } from '@/enum/cacheEnum';
import { IMenu } from '#/menu';
import router from '@/router';
import utils from '@/utils';
import { ref } from 'vue';

class Menu {
  public menus = ref<IMenu[]>([]);
  public historyMenus = ref<IMenu[]>([]);
  public isClose = ref();
  public route = ref(null as null | RouteLocationNormalized);

  constructor() {
    this.menus.value = this.getMenuByRoute();
    this.historyMenus.value = this.getHistoryMenu();
    this.isClose.value = false;
  }

  // 获取历史路由（过滤）
  private getHistoryMenu() {
    const allRoutes = [] as RouteRecordRaw[];
    router.getRoutes().map(r => allRoutes.push(...r.children));
    let historyMenus: IMenu[] = utils.storage.get(CacheEnum.HISTORY_MENUS) ?? ([] as IMenu[]);
    return historyMenus.filter(m => {
      return allRoutes.some(r => r.name === m.route);
    });
  }
  // 切换父级菜单状态
  showOrHideParentMenu(menu: IMenu) {
    this.menus.value.forEach(item => {
      item.isClick = false;
      if (item === menu) item.isClick = true;
    });
  }
  // 显示或者隐藏整个菜单
  showOrHideMenu() {
    this.isClose.value = !this.isClose.value;
  }
  // 设置激活菜单
  setCurrentMenu(route: RouteLocationNormalized) {
    this.menus.value.forEach(menu => {
      menu.isClick = false;
      menu.children?.forEach(menuChild => {
        menuChild.isClick = false;
        if (menuChild.route === route.name) {
          menu.isClick = true;
          menuChild.isClick = true;
        }
      });
    });
  }
  // 根据路由获取菜单
  getMenuByRoute() {
    const parentMenus = this.getParentMenus();
    const tempMenus = parentMenus.map(route => {
      let menu: IMenu = { ...route.meta.menu };
      menu.children = route.children
        .filter(route => route.meta?.menu)
        .map(route => {
          return { ...route.meta?.menu, route: route.name };
        }) as IMenu[];
      return menu;
    });
    return tempMenus.filter(menu => menu.children?.length);
  }

  // 移除路由
  removeHistory(menu: IMenu) {
    const index = this.historyMenus.value.indexOf(menu);
    this.historyMenus.value.splice(index, 1);
    utils.storage.set(CacheEnum.HISTORY_MENUS, this.historyMenus.value);
  }

  // 添加路由
  addHistoryMenu(route: RouteLocationNormalized) {
    if (!route.meta.menu) return;
    this.route.value = route;
    const menu = { ...route.meta?.menu, route: route.name as string };
    const isHas = this.isRepeatMenu(route, this.historyMenus.value);
    if (!isHas) this.historyMenus.value.unshift(menu);
    if (this.historyMenus.value.length > 10) this.historyMenus.value.pop();
    utils.storage.set(CacheEnum.HISTORY_MENUS, this.historyMenus.value);
  }

  // 是否重复菜单
  isRepeatMenu(route: RouteLocationNormalized, allHistoryMenus: IMenu[]) {
    return allHistoryMenus.some(menu => menu.route == route.name);
  }

  // 获取所有的一级路由
  getParentMenus() {
    return router.getRoutes().filter(route => route.children.length && route.meta.menu);
  }
}

export default new Menu();
